/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package evabot.bot;

//package alicebot.bot;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import bitoflife.chatterbean.AliceBot;
import bitoflife.chatterbean.Context;
import bitoflife.chatterbean.parser.AliceBotParser;
import bitoflife.chatterbean.parser.AliceBotParserConfigurationException;
import bitoflife.chatterbean.parser.AliceBotParserException;
import bitoflife.chatterbean.util.Searcher;

/**
 * Classe responsÃ¡vel pela comunicaÃ§Ã£o do Bot com a controladora do framework
 * Chatterbean.
 *
 * @author Alexandre Sales, Danielle Chaves, Maria Isabel Farias
 *
 */
public class EvaBotMother {

	private ByteArrayOutputStream gossip;
	private String contextFile,splitterFile,parentDirectory,aimlFiles,substitutions;

	/**
	 * ConstrÃ³i o Bot-MÃ£e.
	 *
	 * @param contextFile
	 * 		LocalizaÃ§Ã£o do context.xml.
	 * @param splitterFile
	 * 		LocalizaÃ§Ã£o do splitters.xml.
	 * @param substitutions
	 * 		LocalizaÃ§Ã£o do substitutions.xml.
	 * @param parentDirectory
	 * 		LocalizaÃ§Ã£o do diretÃ³rio raÃ­z da base de conhecimento.
	 * @param aimlFiles
	 * 		LocalizaÃ§Ã£o dos arquivos aiml.
	 */
	public EvaBotMother(String contextFile,String splitterFile,String substitutions,String parentDirectory,String aimlFiles) {
		this.contextFile = contextFile;
		this.splitterFile = splitterFile;
		this.substitutions = substitutions;
		this.parentDirectory = parentDirectory;
		this.aimlFiles = aimlFiles;

		setUp();
	}

	/**
	 * Recupera o arquivo context.xml.
	 *
	 * @return O arquivo context.xml.
	 */
	public String getContextFile() {
		return this.contextFile;
	}

	/**
	 * Recupera o arquivo splitters.xml.
	 *
	 * @return O arquivo splitters.xml.
	 */
	public String getSplitterFile() {
		return this.splitterFile;
	}

	/**
	 * Recupera o arquivo substitutions.xml.
	 *
	 * @return O arquivo substitutions.xml.
	 */
	public String getSubstitutions() {
		return this.substitutions;
	}

	/**
	 * Recupera o diretÃ³rio raÃ­z da base de conhecimento.
	 *
	 * @return O diretÃ³rio raÃ­z da base de conhecimento.
	 */
	public String getParentDirectory() {
		return this.parentDirectory;
	}

	/**
	 * Recupera os arquivos AIML.
	 *
	 * @return Os arquivos AIML.
	 */
	public String getAIMLFiles() {
		return this.aimlFiles;
	}

	public void setUp() {
		gossip = new ByteArrayOutputStream();
	}

	public String gossip() {
		return gossip.toString();
	}

	/**
	 * Cria uma instÃ¢ncia do Bot-MÃ£e.
	 *
	 * @return A instÃ¢ncia do Bot-MÃ£e.
	 *
	 * @throws AliceBotParserConfigurationException
	 * 		Caso haja algum arquivo de configuraÃ§Ã£o errado.
	 * @throws FileNotFoundException
	 * 		Caso o arquivo nÃ£o exista.
	 * @throws AliceBotParserException
	 * 		Caso haja algum diretÃ³rio de configuraÃ§Ã£o invÃ¡lido.
	 * @throws IOException
	 * 		Erro de entrada/saida.
	 */
	public AliceBot newInstance() throws AliceBotParserConfigurationException, FileNotFoundException, AliceBotParserException, IOException {
		Searcher searcher = new Searcher();
		AliceBotParser parser = new AliceBotParser();
		AliceBot alice = parser.parse(new FileInputStream(getContextFile()),
									  new FileInputStream(getSplitterFile()),
									  new FileInputStream(getSubstitutions()),
									  searcher.search(getParentDirectory(), getAIMLFiles()));
		Context context = alice.getContext();
		context.outputStream(gossip);
		return alice;
	}
}
